
package com.tools.cleanmaster.ui.view.functionpage;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;

import com.tools.cleanmaster.R;
import com.tools.cleanmaster.taskmain.ProcessItem;

import java.util.List;


public class AppsIconListView extends FrameLayout {

    class GridViewAdapter extends BaseAdapter {
        private List<ProcessItem> mList;

        public GridViewAdapter(List<ProcessItem> list) {
            mList = list;
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView mImageView;
            if (convertView == null) {
                mImageView = new ImageView(mContext);
                mImageView
                        .setLayoutParams(new GridView.LayoutParams(ICON_SIZE, ICON_SIZE));
            } else {
                mImageView = (ImageView) convertView;
            }
            mImageView.setImageDrawable(mList.get(position).icon);
            return mImageView;
        }

        public void clear() {
            mList.clear();
            notifyDataSetChanged();
        }

    }

    private int ICON_SIZE;

    private GridView mGridView;
    private GridViewAdapter mAdapter;
    private Animation mFlipAnimation;
    private LayoutAnimationController mFlipAnimationController;
    private Context mContext;
    private List<ProcessItem> mProcessItems;

    public AppsIconListView(Context ctx) {
        super(ctx);
        init();
    }

    public AppsIconListView(Context ctx, AttributeSet attrs) {
        super(ctx, attrs);
        init();
    }

    private void init() {
        mContext = getContext();
        inflate(getContext(), R.layout.app_icon_grid_layout, this);

        mFlipAnimation = AnimationUtils.loadAnimation(mContext, com.tools.cleanmaster.R.anim.appicon_out_anim);
        mFlipAnimation.setFillAfter(true);
        mFlipAnimationController = new LayoutAnimationController(mFlipAnimation);
        mFlipAnimationController.setOrder(LayoutAnimationController.ORDER_NORMAL);
        mFlipAnimationController.setDelay(0.4f);
    }

    @Override
    public void onFinishInflate() {
        super.onFinishInflate();
        mGridView = findViewById(R.id.app_icon_grid);
        ICON_SIZE = getResources().getDimensionPixelOffset(R.dimen.runapp_iconshow_size);
    }

    public void clearAppAnimation(final Animation.AnimationListener listener) {
        mGridView.setLayoutAnimation(mFlipAnimationController);
        mGridView.setLayoutAnimationListener(listener);
        mGridView.postDelayed(new Runnable() {
            @Override
            public void run() {
                mGridView.startLayoutAnimation();
            }
        }, 50);
    }

    public void setAppsIcons(List<ProcessItem> processItems) {
        mProcessItems = processItems;
        mAdapter = new GridViewAdapter(mProcessItems);
        mGridView.setAdapter(mAdapter);
    }
}
